<template>
  <div class="mod-config">
    <basic-container>
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="优惠券名称" prop="couponName">
          <el-input
            v-model.trim="queryParams.couponName"
            placeholder="请输入优惠券名称"
            clearable
            size="small"
            @keyup.enter.native="getDataList"
          />
        </el-form-item>

        <el-form-item label="启用状态:" prop="status">
          <el-select
            v-model="queryParams.status"
            filterable
            placeholder="请选择启用状态"
            clearable
            size="small"
            class="margin-left-xs"
          >
            <el-option
              v-for="dict in statusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="getDataList"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-button
            v-if="permissions.shop_coupon_add"
            icon="el-icon-plus"
            type="primary"
            @click="addOrUpdateHandle()"
            >新增</el-button
          >
        </el-form-item>
      </el-form>

      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading">
          <el-table-column
            type="index"
            header-align="center"
            :index="indexMethod"
            align="center"
            label="编号"
          >
          </el-table-column>
          <el-table-column
            prop="couponName"
            header-align="center"
            align="center"
            label="优惠券名称"
            width="200px"
          >
          </el-table-column>
          <el-table-column
            prop="couponCate"
            header-align="center"
            align="center"
            label="种类"
            width="120px"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.couponCate === '1'">用户领取</span>
              <span v-else-if="scope.row.couponCate === '2'">手动发放</span>
              <span v-else-if="scope.row.couponCate === '3'">付费卷</span>
              <span v-else>新人卷</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="couponType"
            header-align="center"
            align="center"
            label="类型"
            width="120px">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.couponType === '1'">代金券</el-tag>
              <el-tag v-else>折扣券</el-tag>
            </template>
          </el-table-column>

          <el-table-column
            prop="orderPrice"
            header-align="center"
            align="center"
            label="订单金额"
            :formatter="formatMoney"
            width="120px">
          </el-table-column>

          <el-table-column
            prop="orderPrice"
            header-align="center"
            align="center"
            label="优惠减免"
            width="120px">
            <template slot-scope="scope">
              <template v-if="scope.row.couponType == '1'">
                <span>-</span>
                <span style="margin-left: 5px">{{scope.row.deductionPrice | formatMoneyToYuanFilter}}</span>
              </template>
              <template v-else-if="scope.row.couponType == '2'">
                <span >{{scope.row.discountRate | formatRateFilter}}</span>
                <span style="margin-left: 2px">折</span>
              </template>
              <template v-else>
                <span>未知</span>
              </template>
            </template>
          </el-table-column>
          <el-table-column
            prop="status"
            header-align="center"
            align="center"
            label="是否显示"
            width="120px">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.status === '1'">启用</el-tag>
              <el-tag type="info" v-else>关闭</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="优惠券时间区间" align="center" prop>
            <el-table-column
              prop="expirationType"
              header-align="center"
              align="center"
              label="到期类型"
              width="120px">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.expirationType === '1'">领券后生效</el-tag>
                <el-tag v-else>固定时间段</el-tag>
              </template>
            </el-table-column>
            <el-table-column
              label="领券后失效时间"
              align="center"
              width="150"
            >
              <template slot-scope="scope">
                <span v-if="scope.row.expirationType === '1'">领取后{{scope.row.effectiveDays}}天内有效</span>
                <span v-else>-</span>
              </template>
            </el-table-column>
            <el-table-column
              label="开始时间"
              align="center"
              prop="refundTime"
              width="150"
            >
              <template slot-scope="scope">
                <span v-if="scope.row.expirationType === '2'">{{formatDate(scope.row.effectiveStartDate)}}</span>
                <span v-else>-</span>
              </template>
            </el-table-column>
            <el-table-column
              label="结束时间"
              align="center"
              prop="refundTime"
              width="150"
            >
              <template slot-scope="scope">
                <span v-if="scope.row.expirationType === '2'">{{formatDate(scope.row.effectiveEndDate)}}</span>
                <span v-else>-</span>
              </template>
            </el-table-column>
          </el-table-column>
          
          <el-table-column
            prop="orderBy"
            header-align="center"
            align="center"
            label="排序"
            width="120px"
          >
          </el-table-column>
          <el-table-column
            prop="createTime"
            header-align="center"
            align="center"
            label="创建时间"
            width="150px"
          >
          </el-table-column>
          <el-table-column header-align="center" align="center" label="操作" width="120px" fixed="right" >
            <template slot-scope="scope">
              <el-button
                v-if="permissions.shop_coupon_edit"
                type="text"
                size="small"
                icon="el-icon-edit"
                @click="addOrUpdateHandle(scope.row.id)"
                >修改</el-button
              >
              <el-button
                v-if="permissions.shop_coupon_del"
                type="text"
                size="small"
                icon="el-icon-delete"
                @click="deleteHandle(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="queryParams.pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="queryParams.pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <EditForm
        v-if="visible"
        ref="addOrUpdate"
        @refreshDataList="getDataList"
        :visible.sync="visible"
      ></EditForm>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, delObj } from "@/api/shop_operate/coupon";
import EditForm from "./edit";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        couponName:'',
        status:''
      },
      dataList: [],
      // 优惠券状态 1: 启动 2: 关闭
      statusList: [
        {
          value: "1",
          label: "启动",
        },
        {
          value: "0",
          label: "关闭",
        },
      ],

      totalPage: 0,
      dataListLoading: false,
      visible: false,
    };
  },
  filters:{
    
  },
  components: {
    EditForm,
  },
  created() {
    this.getDataList();
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  methods: {
    formatDate(val){
      const date = this.dayjs(val).format('YYYY-MM-DD')
      return date
    },
    /**
     * 获取数据列表
     */
    getDataList() {
      this.dataListLoading = true;
      fetchList(
        Object.assign({
          couponName: this.queryParams.couponName,
          status: this.queryParams.status,
          current: this.queryParams.pageIndex,
          size: this.queryParams.pageSize
        })
      ).then((response) => {
        this.dataListLoading = false;
        this.dataList = response.data.data.records;
        this.totalPage = response.data.data.total;
      });
    },

    /**
     * 每页数
     */
    sizeChangeHandle(val) {
      this.queryParams.pageSize = val;
      this.queryParams.pageIndex = 1;
      this.getDataList();
    },

    /**
     * 当前页
     */
    currentChangeHandle(val) {
      this.queryParams.pageIndex = val;
      this.getDataList();
    },

    /**
     * 新增 / 修改
     */
    addOrUpdateHandle(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },

    /**
     * 删除
     */
    deleteHandle(id) {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delObj(id);
        })
        .then((data) => {
          this.$message.success("删除成功");
          if(this.dataList.length <= 1 && this.queryParams.pageIndex > 1){
            this.queryParams.pageIndex--;
          }
          this.getDataList();
        })
        .catch(() => {});
    },

    /**
     * 重置
     */
    resetQuery() {
      this.queryParams.pageIndex = 1;
      this.queryParams.pageSize = 10;
      this.resetForm("queryForm");
      this.getDataList();
    },

    indexMethod(index) {
      return (
        (this.queryParams.pageIndex - 1) * this.queryParams.pageSize + index + 1
      );
    },
  },
};
</script>
